package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.basic.Agent;
import cc.yugu.yhdw.common.domain.hdw.Agency;
import cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivideDetail;
import cc.yugu.yhdw.common.domain.hdw.WithdrawCash;
import cc.yugu.yhdw.common.domain.hdw.SupplierInfo;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.AgencyMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.SupplierMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.WithdrawCashMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.CabinetBoxMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 金额提现业务
 * 
 * @Date:Created in 2018/7/10 17:25
 * @Author:
 * @Modified By: zjn 2018/7/10 17:25
 */ 
@Service
public class WithdrawCashService extends AbstractService {
    @Autowired
    WithdrawCashMapper withdrawCashMapper;
    @Autowired
    AgentMapper agentMapper;
    @Autowired
    AgencyMapper agencyMapper;
    @Autowired
    SupplierMapper supplierMapper;
    /**
    * 分页查询
    * 
    * @Param: [withdrawCash]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/10  17:25
    */ 
    public Page findPage(WithdrawCash withdrawCash) {
        Page page = withdrawCash.buildPage();
        page.setTotalItems(withdrawCashMapper.findPageCount(withdrawCash));
        withdrawCash.setBeginIndex(page.getOffset());

        List<WithdrawCash> withdrawCashList = withdrawCashMapper.findPageResult(withdrawCash);
        page.setResult(withdrawCashList);

        ExchangeAmountMoneyDivideDetail.OrgType[] types = ExchangeAmountMoneyDivideDetail.OrgType.values();
        for (WithdrawCash e : withdrawCashList) {
            for (int i = 0; i < types.length; i++) {
                if (e.getOrgType() == types[i].getValue()) {
                    e.setOrgTypeName(types[i].getName());
                }
            }
            if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENT.getValue()) {
                e.setOrgIdName(agentMapper.find(e.getOrgId()).getAgentName());
            } else if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENCY.getValue()) {
                e.setOrgIdName(agencyMapper.find(e.getOrgId()).getAgencyName());
            } else if (e.getOrgType() != ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
                e.setOrgIdName(supplierMapper.find(e.getOrgId()).getSupplierName());
            }
        }
        return page;
    }
    /**
    * 插入
    * 
    * @Param: [withdrawCash]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:26
    */ 
    public ExtResult insert(WithdrawCash withdrawCash) {
        withdrawCash.setMoney(withdrawCash.getMoney()*100);
        if (withdrawCash.getMoney()<0){
            withdrawCash.setMoney(-withdrawCash.getMoney());
        }
        if (withdrawCash.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENT.getValue()) {
            Agent agent = agentMapper.find(withdrawCash.getOrgId());
            if (agent.getBalance() >= withdrawCash.getMoney()) {
                agentMapper.updateBalance(agent.getId(),agent.getBalance() - withdrawCash.getMoney());
            } else {
                return ExtResult.failResult("账户余额不足");
            }
        } else if (withdrawCash.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENCY.getValue()) {
            Agency agency = agencyMapper.find(withdrawCash.getOrgId());
            if (agency.getBalance() >= withdrawCash.getMoney()) {
                agencyMapper.updateBalance(agency.getId(),agency.getBalance() - withdrawCash.getMoney());
            } else {
                return ExtResult.failResult("账户余额不足");
            }
        }

        withdrawCash.setCreateTime(new Date());
        withdrawCashMapper.insert(withdrawCash);
        return ExtResult.successResult();
    }
    /**
    * 更新
    * 
    * @Param: [withdrawCash]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:26
    */ 
    public ExtResult update(WithdrawCash withdrawCash) {
        withdrawCashMapper.update(withdrawCash);
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id]
    * @Return: int
    * @Author By: zjn 2018/7/10  17:26
    */ 
    public int delete(int id) {
        return withdrawCashMapper.delete(id);
    }
    /**
    * 查询（按条件）
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.WithdrawCash
    * @Author By: zjn 2018/7/10  17:27
    */ 
    public WithdrawCash find(int id) {
        WithdrawCash withdrawCash = withdrawCashMapper.find(id);
        ExchangeAmountMoneyDivideDetail.OrgType[] types = ExchangeAmountMoneyDivideDetail.OrgType.values();
        for (int i = 0; i < types.length; i++) {
            if (withdrawCash.getOrgType() == types[i].getValue()) {
                withdrawCash.setOrgTypeName(types[i].getName());
            }
        }
        if (withdrawCash.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENT.getValue()) {
            withdrawCash.setOrgIdName(agentMapper.find(withdrawCash.getOrgId()).getAgentName());
        } else if (withdrawCash.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENCY.getValue()) {
            withdrawCash.setOrgIdName(agencyMapper.find(withdrawCash.getOrgId()).getAgencyName());
        } else if (withdrawCash.getOrgType() != ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
            withdrawCash.setOrgIdName(supplierMapper.find(withdrawCash.getOrgId()).getSupplierName());
        }
        return withdrawCash;
    }
}
